<?php

class Ajax extends AjaxController implements IExceptionHandler {

    public function ExceptionHandler(Exception $e) {
        $this->view(['exception' => $e->getMessage()]);
    }

    public function POST_ajax_refresh_manager_form() {
        $PARAM_id_client_account = lib_check_param_post($_POST['id_client_account']);
        $PARAM_id_user = lib_check_param_post($_POST['id_manager']);

        $db_name = $this->user->obj_Account->get_db_name($PARAM_id_client_account);

        $dataUser = $this->user->get_user_data($PARAM_id_user, $db_name);

        $this->view([
            'user_firstname' => $dataUser[0][2],
            'user_profile' => $dataUser[0][14],
        ]);
    }

    public function POST_ajax_refresh_client_linked_list_update($id) {
        $db_name = $this->user->obj_Account->get_db_name($id);

        $dataUser = $this->user->get_all_users_name($db_name);
        $dataProfile = $this->user->get_list_profile($db_name);
        $dataCorporateNode = $this->user->obj_Account->obj_Corporate->get_list_node($db_name);
        $dataManager = $this->user->get_list_manager($db_name);

        $result = [
            'id_client_account' => $id,
            'users' => gui_form_select_options($dataUser, $dataUser[0][0]),
            'roles' => gui_form_select_options($dataProfile),
            'nodes' => gui_form_select_options($dataCorporateNode),
            'all_users_except_level_1' => gui_form_select_options($dataManager, $dataManager[0][0]),
            'manager_profile' => $dataManager[0][3],
            'manager_firstname' => $dataManager[0][2],
        ];

        if ($dataUser[0] != FALSE) {

            $dataUserDetail = $this->user->get_user_data($dataUser[0][0], $db_name);
            $result['user_detail'] = $dataUserDetail;

            $dataUserLogin = $this->user->get_id_pwd_user($dataUser[0][0]);
            $result['user_login'] = $dataUserLogin;
        }

        $this->view($result);
    }

    public function POST_ajax_refresh_user_form() {
        $PARAM_id_client_account = lib_check_param_post($_POST['id_client_account']);
        $PARAM_id_user = lib_check_param_post($_POST['id_user']);

        $db_name = $this->user->obj_Account->get_db_name($PARAM_id_client_account);

        $dataUserDetail = $this->user->get_user_data($PARAM_id_user, $db_name);
        $dataUserLogin = $this->user->get_id_pwd_user($PARAM_id_user);

        $result = [
            'user_detail' => $dataUserDetail,
            'user_login' => $dataUserLogin,
        ];

        if ($dataUserDetail[0][6] != NULL) {
            $dataManager = $this->user->get_user_data($dataUserDetail[0][6], $db_name);
            $result['user_manager'] = $dataManager;
        }

        $this->view($result);
    }

    public function POST_ajax_refresh_client_linked_list_creation($id) {
        $db_name = $this->user->obj_Account->get_db_name($id);

        $dataProfile = $this->user->get_list_profile($db_name);
        $dataCorporateNode = $this->user->obj_Account->obj_Corporate->get_list_node($db_name);
        $dataManager = $this->user->get_list_manager($db_name);

        $this->view([
            'roles' => gui_form_select_options($dataProfile),
            'nodes' => gui_form_select_options($dataCorporateNode),
            'all_users_except_level_1' => gui_form_select_options($dataManager, $dataManager[0][0]),
            'manager_profile' => $dataManager[0][3],
            'manager_firstname' => $dataManager[0][2],
        ]);
    }

    public function POST_ajax_refresh_manager_form_creation() {
        $PARAM_id_user = lib_check_param_post($_POST['id_manager']);
        $PARAM_id_client_account = lib_check_param_post($_POST['id_client_account']);

        $db_name = $this->user->obj_Account->get_db_name($PARAM_id_client_account);

        $dataUser = $this->user->get_user_data($PARAM_id_user, $db_name);

        $this->view([
            'id_user' => $dataUser[0][0],
            'user_name' => $dataUser[0][1],
            'user_firstname' => $dataUser[0][2],
            'user_profile' => $dataUser[0][14],
        ]);
    }

}
